#!/usr/bin/python
# -*- coding: utf-8 -*-

import os,urllib2,socket,Image,cStringIO

class FileIO():
    
    @staticmethod
    def write(filename,data):
        f=open(filename,"wb")
        f.write(data)
        f.close()
        
    @staticmethod
    def read(filename):
        f=open(filename,"rb")
        data=f.read()
        f.close()
        return data
        
class NetIO():
    @staticmethod
    def read(url,timeout=12,buffer=16):
        stream=cStringIO.StringIO()
        res=urllib2.urlopen(url,timeout=timeout)
        try:
            data=res.read(buffer*1024)
            while data:
                stream.write(data)
                data=res.read()  
        finally:
            res.close()
        return stream.getvalue()
    
class File():
    
    def __init__(self,filename):
        self.fullname=os.path.abspath(filename) 
        #文件路径，注意，最后没有斜杠和反斜杠
        self.pathname=os.path.dirname(self.fullname)
        #文件名
        self.filename=os.path.basename(self.fullname)
        # 返回没有扩展名的文件名，扩展名
        self.shortname,self.extname=os.path.splitext(self.filename) #("temp",".jpg")

    @staticmethod
    def getFullname(filename):
        return os.path.abspath(filename) 
        
    @staticmethod
    def getPathname(filename):
        return os.path.dirname(filename)
        
    @staticmethod
    def getFilename(filename):
        return os.path.basename(filename)
        
    @staticmethod
    def getShortname(filename):
        return os.path.splitext(File.getFilename(filename))[0]
        
    @staticmethod
    def getExtname(filename):
        return os.path.splitext(File.getFilename(filename))[1]
        
    def exists(self):
        return os.path.exists(self.fullname)
        
    def mkdir(self):
        if os.path.exists(self.fullname):
            return
        os.mkdir(self.fullname)
        
    def read(self,fn=FileIO.read):
        return fn(self.fullname)
        
    def write(self,data,fn=FileIO.write):
        fn(self.fullname,data)
    
class ImageFile():
    @staticmethod    
    def thumbnail(data,size=(240,240)):
        im = Image.open(cStringIO.StringIO(data))
        im.thumbnail(size)
        out=cStringIO.StringIO()
        im.save(out,"JPEG")
        return out.getvalue()
        
    @staticmethod    
    def convert(data,ext="JPEG"):
        im = Image.open(cStringIO.StringIO(data))
        out=cStringIO.StringIO()
        im.save(out,ext)
        return out.getvalue()

class Media():
    @staticmethod
    def speak(data):
        if data is None: return
        #2.创建合成器对象，解析出最初的几帧音频数据
        import pymedia.muxer as muxer
        dm= muxer.Demuxer('mp3')
        frames= dm.parse( data )
        
        #3.根据解析出来的 Mp3 编码信息，创建解码器对象
        import pymedia.audio.acodec as acodec
        dec= acodec.Decoder( dm.streams[ 0 ] )
        
        #4.解码第一帧音频数据
        r= dec.decode(data)
        
        #5.创建音频输出对象
        import pymedia.audio.sound as sound
        snd= sound.Output( r.sample_rate, r.channels, sound.AFMT_S16_LE )
        
        #6.播放
        if r: 
            snd.play( r.data )
            
        #8.延时，直到播放完毕
        import time
        while snd.isPlaying(): time.sleep( .5 )

        
if __name__=='__main__':

    print File.getFullname("C:\\System\\")
    print File.getPathname("C:\\System\\")
    pass  


        